package com.dayuanit.dy14.rpc.demo.net;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    public static void start() throws Exception {
        ServerSocket serverSocket = new ServerSocket(10001);
        System.out.println("server start...");

        Socket clientSocket = serverSocket.accept();
        //TODOO ....

        ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());
        String className = ois.readUTF();//类的全名称
        String methodName = ois.readUTF();//方法名
        Object paramClass = ois.readObject();//方法入参类型
        Object paramObject = ois.readObject();//方法实际入参值

        Class clazz = Class.forName(className);

        Object targetObject = clazz.newInstance();
        Method targetMethod = clazz.getMethod(methodName, (Class[]) paramClass);

        Object reusltObject = targetMethod.invoke(targetObject, (Object[]) paramObject);

        ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream());
        oos.writeObject(reusltObject);
        oos.flush();
    }
}
